// 给我100000转化为10:00
import dayjs from 'dayjs';

export function convertToHoursMinute(value) {
  if (typeof value !== 'string') {
    return '';
  }
  const newValue = value.slice(0, 4);

  return `${newValue.slice(0, 2)}:${newValue.slice(2)}`;
}

// 给我10：00转为100000
export function hourMinuteSecond(value) {
  const newValue = value.replace(':', '');
  return `${newValue}00`;
}

// 年月日时分秒具体时间转换
export function timeDays(times) {
  return (
    times.substring(0, 4) +
    '-' +
    times.substring(4, 6) +
    '-' +
    times.substring(6, 8) +
    ' ' +
    times.substring(8, 10) +
    ':' +
    times.substring(10, 12) +
    ':' +
    times.substring(12, 14)
  );
}

// 获取一周内的日期范围
export function getLastWeekRange(format = 'YYYYMMDDHHmmss') {
  const startOfWeek = dayjs();
  const endOfWeek = startOfWeek.add(6, 'day').endOf('day');
  return [startOfWeek.format(format), endOfWeek.format(format)];
}

// 获取一个月内的日期范围
export function getLastMonthRange(format = 'YYYYMMDDHHmmss') {
  const startOfMonth = dayjs();
  const endOfMonth = startOfMonth.add(1, 'month').endOf('day');
  return [startOfMonth.format(format), endOfMonth.format(format)];
}

// 获取本周内的日期范围
export function getCurrentWeekRange(format = 'YYYYMMDDHHmmss') {
  const endOfWeek = dayjs().endOf('week');
  const startOfWeek = dayjs().startOf('week').endOf('day');
  return [startOfWeek.format(format), endOfWeek.format(format)];
}

// 给我YYYYMMDDHHmmss转化为YYYY.MM.DD
export function formattedDate(dateString: string) {
  return dayjs(dateString, { format: 'YYYYMMDDHHmmss' }).format('YYYY.MM.DD');
}

// 给我数据给我当前什么格式转化为什么格式
export function mostFormattedDate(dateString: string, oldFormat, newFormat) {
  return dayjs(dateString, { format: oldFormat }).format(newFormat);
}

export function calculateDaysDifference(startTime, endTime) {
  const newStartTime = startTime.slice(0, 8);
  const newEndTime = endTime.slice(0, 8);
  const startDateTime = dayjs(newStartTime, { format: 'YYYYMMDD' });
  const endDateTime = dayjs(newEndTime, { format: 'YYYYMMDD' });
  console.log(startDateTime, endDateTime);
  // 计算天数差异
  const daysDifference = endDateTime.diff(startDateTime, 'day') + 1;
  console.log(daysDifference, '你好');
  return daysDifference;
}
